<?php
include "./core/help.php";
spl_autoload_register(['App','autoload']);
App::run($argv);
/**
 * Class App
 * php index.php --option=fff --option-a=666  -s=fff
 */
class App {

    private static $argv = [];
    private static $options = [];
    private static $param = [];
    public static $debug = 0;
    private static $_dbs = [];
    private static $_config = [];

    public static function run($argv)
    {
        self::$argv = $argv ;
        self::parseArgs();
        self::runAction();
    }

    private static function parseArgs()
    {
        foreach(self::$argv as $arg){
            if($arg == 'index.php'){
                continue ;
            }
            if(substr($arg , 0 , 2) == "--"){
                $s = substr($arg , 2) ;
                if($s == ""){
                    continue;
                }
                $arr = explode("=",$s) ;
                self::$options[str_replace('-',"_",$arr[0])] = $arr[1] ;
            }else if(substr($arg , 0 , 1) == "-"){

            }else{
                self::$param[] = $arg ;
            }
        }
    }

    public static function getOptions($key = '')
    {
        if($key != ''){
            return self::$options[$key];
        }
        return self::$options;
    }

    public static function getParam($key = null)
    {
        if($key != null){
            return self::$param[$key];
        }
        return self::$param;
    }

    private static function runAction()
    {
        $controller = 'help';
        $action = 'index' ;
        if(!empty(self::$param)){
            $arr = explode(":" , self::$param[0]) ;
            $controller = $arr[0] ;
            if(isset($arr[1])){
                $action = $arr[1] ;
            }
        }

        $controller = strToclassName($controller);
        $className = "\\app\\controller\\{$controller}Controller";
        $controllerInstance = new $className ;
        $method = "{$action}Action" ;
        if(method_exists($controllerInstance , $method)){
            call_user_func([$controllerInstance , $method]) ;
        }else{
            self::error("Action not found");
        }
    }

    private static function error($key,$value = null)
    {
        if(App::$debug == -1){
            return ;
        }
        echo  "----------------------------> {$key} <----------------------------\n";
        if($value != null){
            print_r($value) ;
        }
        echo "\n\n";
    }

    public static function autoload($class)
    {
        //tag("autoload" , $class) ;
        $filename = str_replace('\\' ,'/' , $class) .".php";
        if(substr($filename , 0 , 4 ) == "app/"){
            $filename = substr($filename , 4) ;
        }
        $filename = __DIR__.'/'.$filename ;
        //tag('filename' , $filename) ;
        include  $filename;
    }

    /**
     * @param string $name
     * @return \app\core\DB
     */
    public static function db($name = 'default')
    {
        if(!isset(self::$_dbs[$name])){
            $conf = App::config('database');
            $conf = $conf[$name] ;
            self::$_dbs[$name] = new \app\core\DB($conf);
        }
        return self::$_dbs[$name] ;
    }

    public static function config($key = '')
    {
        //tag("config",__DIR__."/config/{$key}.php");
        if(!isset(self::$_config[$key])){
            self::$_config[$key] = include  __DIR__."/config/{$key}.php";
        }
        return self::$_config[$key] ;
    }

    public static function loadView($tpl , $controller , $data)
    {
        extract($data);
        include __DIR__."/view/{$tpl}.php";
    }

    public static function getAppPath(){
        return __DIR__.'/' ;
    }
}